<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 矩阵最大值（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给定一个仅包含0和1的N*N二维矩阵&#xff0c;请计算二维矩阵的最大值&#xff0c;计算规则如下&#xff1a;</p> 
<p>1、 每行元素按下标顺序组成一个二进制数&#xff08;下标越大越排在低位&#xff09;&#xff0c;二进制数的值就是该行的值。矩阵各行值之和为矩阵的值。<br /> 2、允许通过向左或向右整体循环移动每行元素来改变各元素在行中的位置。</p> 
<p>比如&#xff1a;<br /> [1,0,1,1,1]向右整体循环移动2位变为[1,1,1,0,1]&#xff0c;二进制数为11101&#xff0c;值为29。<br /> [1,0,1,1,1]向左整体循环移动2位变为[1,1,1,1,0]&#xff0c;二进制数为11110&#xff0c;值为30。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>1、输入的第一行为正整数&#xff0c;记录了N的大小&#xff0c;0 &lt; N &lt;&#61; 20。<br /> 2、输入的第2到N&#43;1行为二维矩阵信息&#xff0c;行内元素边角逗号分隔。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>矩阵的最大值。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:658px;"><tbody><tr><td style="width:103px;">输入</td><td style="width:554px;">5<br /> 1,0,0,0,1<br /> 0,0,0,1,1<br /> 0,1,0,1,0<br /> 1,0,0,1,1<br /> 1,0,1,0,1</td></tr><tr><td style="width:103px;">输出</td><td style="width:554px;">122</td></tr><tr><td style="width:103px;">说明</td><td style="width:554px;"> <p>第一行向右整体循环移动1位&#xff0c;得到本行的最大值[1,1,0,0,0]&#xff0c;二进制值为11000&#xff0c;十进制值为24。</p> <p>第二行向右整体循环移动2位&#xff0c;得到本行的最大值[1,1,0,0,0]&#xff0c;二进制值为11000&#xff0c;十进制值为24。</p> <p>第三行向左整体循环移动1位&#xff0c;得到本行的最大值[1,0,1,0,0]&#xff0c;二进制值为10100&#xff0c;十进制值为20。</p> <p>第四行向右整体循环移动2位&#xff0c;得到本行的最大值[1,1,1,0,0]&#xff0c;二进制值为11100&#xff0c;十进制值为28。</p> <p>第五行向右整体循环移动1位&#xff0c;得到本行的最大值[1,1,0,1,0]&#xff0c;二进制值为11010&#xff0c;十进制值为26。</p> <p>因此&#xff0c;矩阵的最大值为122。</p> </td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题数量级不大&#xff0c;可以考虑暴力解法&#xff0c;即双重for。</p> 
<p>外层for&#xff0c;遍历每一行。</p> 
<p>内层for&#xff0c;将每一行当成双端队列结构&#xff0c;每次尾部出队元素再头部入队&#xff0c;得到一个新状态的二进制字符串&#xff0c;进行N次&#xff0c;即可得到该行所有二进制字符串&#xff0c;我们只需要计算出每个二进制字符串的十进制数&#xff0c;保留最大值&#xff0c;作为该行结果。</p> 
<p>最后对每一行的最大十进制数求和即为题解。</p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; Integer.parseInt(sc.nextLine());

    int ans &#61; 0;

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      LinkedList&lt;Integer&gt; dq &#61;
          Arrays.stream(sc.nextLine().split(&#34;,&#34;))
              .map(Integer::parseInt)
              .collect(Collectors.toCollection(LinkedList::new));

      int max &#61; getVal(dq);

      for (int j &#61; 1; j &lt; n; j&#43;&#43;) {
        dq.addFirst(dq.removeLast());
        max &#61; Math.max(max, getVal(dq));
      }

      ans &#43;&#61; max;
    }

    System.out.println(ans);
  }

  public static int getVal(LinkedList&lt;Integer&gt; dq) {
    StringBuilder sb &#61; new StringBuilder();
    for (Integer v : dq) sb.append(v);
    return Integer.parseInt(sb.toString(), 2);
  }
}
</code></pre> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
let n;
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 1) {
    n &#61; lines[0] - 0;
  }

  if (n &amp;&amp; lines.length &#61;&#61;&#61; n &#43; 1) {
    lines.shift();
    const matrix &#61; lines.map((line) &#61;&gt; line.split(&#34;,&#34;).map(Number));
    console.log(getResult(n, matrix));
    lines.length &#61; 0;
  }
});

function getResult(n, matrix) {
  let ans &#61; 0;

  for (let i &#61; 0; i &lt; n; i&#43;&#43;) {
    let max &#61; parseInt(matrix[i].join(&#34;&#34;), 2);
    for (let j &#61; 1; j &lt; n; j&#43;&#43;) {
      matrix[i].unshift(matrix[i].pop());
      max &#61; Math.max(max, parseInt(matrix[i].join(&#34;&#34;), 2));
    }
    ans &#43;&#61; max;
  }

  return ans;
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())
matrix &#61; [input().split(&#34;,&#34;) for _ in range(n)]


# 算法入口
def getResult():
    ans &#61; 0

    for i in range(n):
        maxV &#61; int(&#34;&#34;.join(matrix[i]), 2)
        for j in range(1, n):
            matrix[i].insert(0, matrix[i].pop())
            maxV &#61; max(maxV, int(&#34;&#34;.join(matrix[i]), 2))
        ans &#43;&#61; maxV

    return ans


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<p> </p> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

#define MAX_SIZE 20
#define MAX(a,b) a &gt; b ? a : b

int getVal(int dq[], int dq_size);
void move(int dq[], int dq_size);

int main() {
    int n;
    scanf(&#34;%d&#34;, &amp;n);

    int ans &#61; 0;

    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        int dq[MAX_SIZE];

        for(int j &#61; 0; j &lt; n; j&#43;&#43;) {
            scanf(&#34;%d&#34;, &amp;dq[j]);
            getchar();
        }

        int max &#61; getVal(dq, n);
        for(int j&#61;1; j&lt;n; j&#43;&#43;) {
            move(dq, n);
            max &#61; MAX(max, getVal(dq, n));
        }

        ans &#43;&#61; max;
    }

    printf(&#34;%d\n&#34;, ans);

    return 0;
}

void move(int dq[], int dq_size) {
    int tmp &#61; dq[0];

    for(int i&#61;1; i&lt;dq_size; i&#43;&#43;) {
        dq[i-1] &#61; dq[i];
    }

    dq[dq_size-1] &#61; tmp;
}

int getVal(int dq[], int dq_size) {
    int ans &#61; 0;

    for(int i&#61;0; i&lt;dq_size; i&#43;&#43;) {
        ans &#43;&#61; dq[i] * (1 &lt;&lt; (dq_size - i - 1));
    }

    return ans;
}</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>